<?php 

defined('BASEPATH') OR exit('No direct script access allowed');

class Common{


	public function __construct()
	{
		

	}

    /**
	 * 输出并跳转
	 */
	public function alert_msg_url($msg,$url='')
	{
		$str = "<script>alert('{$msg}');";

		if(!empty($url))
		{

			$str .= "location.href='$url';</script>";

		}
		else
		{

			$str .= '</script>';

		}
		
		return $str;
	}

	/**
	 * 输出并跳转
	 */
	public function link_url($url='')
	{
		$str = "<script>";

		if(!empty($url))
		{

			$str .= "location.href='$url';</script>";

		}
		else
		{

			$str .= '</script>';

		}
		
		return $str;
	}
	
	/**
	 * 输出并返回
	 */
	public function alert_msg_goback($msg)
	{

		$str = "<script>alert('{$msg}');";

		$str .= "history.go(-1);</script>";

		return $str;
	}

	/**
	 * 登录验证
	 */
	public function check_login()
	{
		if(!isset($_SESSION['user']))
		{

			//exit($this->alert_msg_url('请重新登录',site_url('login')));
			exit($this->link_url(site_url('login')));
		}

	}
	public function check_login_admin()
	{
		if(!isset($_SESSION['admin']))
		{

			exit($this->link_url(site_url('admin/login')));

		}

	}
	public function check_login_mobile()
	{
		if(!isset($_SESSION['mobile']))
		{

			exit($this->alert_msg_url('请重新登录',site_url('mobile/login')));

		}

	}

	/**
	 * 等级
	 */
	public function score_level()
	{
		$arr = array(
			array('id'=>1,'title'=>'初级','first_day_rate'=>2,   'lv_day_rate'=>1,  'score'=>'100'),
			array('id'=>2,'title'=>'中级','first_day_rate'=>2.5, 'lv_day_rate'=>1, 'score'=>'300'),
			array('id'=>3,'title'=>'高级','first_day_rate'=>3,   'lv_day_rate'=>1,   'score'=>'700'),
			array('id'=>4,'title'=>'顶级','first_day_rate'=>3.5, 'lv_day_rate'=>1, 'score'=>'999999999'),
		);
		return $arr;
	}

	/**
	 * 获取用户等级
	 */
	public function get_user_level($score)
	{
		$list = $this->score_level();
		foreach ($list AS $v) {
			if($score < $v['score'])
			{
				return $v;
			}
		}
	}

	/**
	 * 按id获取等级信息
	 */
	public function get_user_level_info($id)
	{
		$list = $this->score_level();

		foreach ($list AS $v) {
			if($id == $v['id'])
			{
				return $v;
			}
		}
		return FALSE;
	}

	/**
	 * 团队等级返点,1-5代返点百分比
	 */
	public function team_lv_rate()
	{
		$arr = array(
			1=>array('title'=>'得道者','max'=>1000,'one'=>6,'two'=>0,'three'=>0,'four'=>0,'five'=>0),
			2=>array('title'=>'天时'  ,'max'=>2000,'one'=>6,'two'=>5,'three'=>0,'four'=>0,'five'=>0),
			3=>array('title'=>'地利'  ,'max'=>3000,'one'=>6,'two'=>5,'three'=>4,'four'=>0,'five'=>0),
			4=>array('title'=>'人和'  ,'max'=>4000,'one'=>6,'two'=>5,'three'=>4,'four'=>3,'five'=>0),
			5=>array('title'=>'君子'  ,'max'=>8000,'one'=>6,'two'=>5,'three'=>4,'four'=>3,'five'=>2),
		);
		return $arr;
	}

	/**
	 * 短信平台
	 */
	public function send_sms($mobile,$content)
	{
		$target = "http://106.ihuyi.cn/webservice/sms.php?method=Submit";
		
		$account = 'cf_liu123123';

		$password = '6568892826a';

		$apikey = '471f2d5e787125c3ca586b366928e64d';

		$post_data = "account=$account&password=$password&mobile=".$mobile."&content=".rawurlencode($content);
		//密码可以使用明文密码或使用32位MD5加密
		$gets =  $this->xml_to_array($this->Post($post_data, $target));
		// if($gets['SubmitResult']['code']==2){
		// 	$_SESSION['mobile'] = $mobile;
		// 	$_SESSION['mobile_code'] = $send_code;
		// }
		return $gets;
	}

	public function Post($curlPost,$url){
		$curl = curl_init();
		curl_setopt($curl, CURLOPT_URL, $url);
		curl_setopt($curl, CURLOPT_HEADER, false);
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($curl, CURLOPT_NOBODY, true);
		curl_setopt($curl, CURLOPT_POST, true);
		curl_setopt($curl, CURLOPT_POSTFIELDS, $curlPost);
		$return_str = curl_exec($curl);
		curl_close($curl);
		return $return_str;
	}

	public function xml_to_array($xml){
		$reg = "/<(\w+)[^>]*>([\\x00-\\xFF]*)<\\/\\1>/";
		if(preg_match_all($reg, $xml, $matches)){
			$count = count($matches[0]);
			for($i = 0; $i < $count; $i++){
			$subxml= $matches[2][$i];
			$key = $matches[1][$i];
				if(preg_match( $reg, $subxml )){
					$arr[$key] = $this->xml_to_array( $subxml );
				}else{
					$arr[$key] = $subxml;
				}
			}
		}
		return $arr;
	}

	/**
	 * ajax返回json
	 */
	public function ajax_return($data)
	{
		header('Content-Type:application/json; charset=utf-8');
		
       	exit(json_encode($data));
	}

	public function getnumChar($length) {
		$str = null;
		$strPol = "0123456789";
		$strPol2 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
		$max = strlen ( $strPol ) - 1;
		$max2 = strlen ( $strPol2 ) - 1;
		
		$str .= $strPol2 [rand ( 0, $max2 )];
		$length = $length - 1;
		for($i = 0; $i < $length; $i ++) {
			$str .= $strPol [rand ( 0, $max )]; // rand($min,$max)生成介于min和max两个数之间的一个随机整数
		}
		
		return $str;
	}

}